// created by noellynch
// May 8, 2009

package com.sceneworker.util.swing.imagelist;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JScrollBar;

import net.miginfocom.swing.MigLayout;

public class SimpleImageList extends JComponent {
	
	public static final int IMAGE_WIDTH = 75;
	public static final int IMAGE_HEIGHT = 75;
	
	private static final int COMPONENT_WIDTH = 150;
	private static final int COMPONENT_HEIGHT = 200;
	
	private		ArrayList<IDrawImage>			mcla_images;
	
	private		Dimension						mcl_compDim;
	
	private		int								mi_curSelectionIndex = -1;
	
	private		IImageSelectionListener			mcl_listener;
	
	public	SimpleImageList() {
		mcla_images = new ArrayList<IDrawImage>();
		mcl_compDim = new Dimension(COMPONENT_WIDTH, COMPONENT_HEIGHT);
		
		buildUI();
	}
	
	public	SimpleImageList(int i_width, int i_height) {
		mcla_images = new ArrayList<IDrawImage>();
		mcl_compDim = new Dimension(i_width, i_height);
		
		buildUI();
	}
	
	private		void	buildUI() {
		setLayout(new MigLayout("inset 5"));
		
		addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent cl_e) {
				Rectangle		lcl_temp = new Rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);
				Point			lcl_pt = cl_e.getPoint();
				
				for(int li_index = 0; li_index < mcla_images.size(); li_index++) {
					if(lcl_temp.contains(lcl_pt)) {
						mi_curSelectionIndex = li_index;
						callListener();
						repaint();
						return;
					}
					
					lcl_temp.y += IMAGE_HEIGHT;
				}
				
				mi_curSelectionIndex = -1;
				callListener();
				repaint();
			}
		});
	}
	
	public	int	getCurrentImageSelectionIndex() {
		return mi_curSelectionIndex;
	}

	private	void	callListener() {
		if(mcl_listener != null) {
			mcl_listener.imageSelected(this);
		}
	}
	
	public	void	setImageSelectionListener(IImageSelectionListener cl_l) {
		mcl_listener = cl_l;
	}
	
	@Override
	public Dimension getPreferredSize() {
		if(mcla_images.size() == 0) {
			return mcl_compDim;
		}
		//System.out.println("request for preferred size..now that we have images " + (mcla_images.size() * IMAGE_HEIGHT));
		return new Dimension(IMAGE_WIDTH, mcla_images.size() * IMAGE_HEIGHT);
	}
	
	@Override
	public Dimension getMaximumSize() {
		return getPreferredSize();
	}

	@Override
	public Dimension getMinimumSize() {
		return getPreferredSize();
	}

	public		void	addDrawImage(IDrawImage cl_im) {
		mcla_images.add(cl_im);
	}

	@Override
	protected void paintComponent(Graphics cl_g) {
		cl_g.clearRect(0, 0, getWidth(), getHeight());
		
		//int		li_y = -mcl_scrollBar.getValue();
		int		li_y = 0;
		int		li_stIndex = 0;
		
		for(; li_stIndex < mcla_images.size(); li_stIndex++) {
			IDrawImage		lcl_im = mcla_images.get(li_stIndex);
			cl_g.drawImage(lcl_im.getImage(), 0, li_y, IMAGE_WIDTH, IMAGE_HEIGHT, this);
			
			if(li_stIndex == mi_curSelectionIndex) {
				cl_g.setColor(Color.green);
				cl_g.drawRect(0, li_y, IMAGE_WIDTH, IMAGE_HEIGHT);
			}
			
			li_y += IMAGE_HEIGHT;
		}
		
	}
	
	public	interface	IImageSelectionListener {
		void	imageSelected(SimpleImageList cl_list);
	}
	
	public	interface	IDrawImage {
		Image		getImage();
	}
}
